//index.js
//获取应用实例
const app = getApp()
const config = require('../../config/config.js')
var ctx = null;
var pixelRatio = null;
Page({
    data: {
        code: null,
        cutImg: '',
        showMore: false,
        userName: '',
        declarationTxt: config.declarationTxt,
        imgSrc: '',
        imgWidth: '',
        imgHeight: '',
        showModal: false
    },
    onLoad: function () {
        this.setData({
            pixelRatio: wx.getSystemInfoSync().pixelRatio
        })
        this.getCode();
    },
    onReady: function () { },
    onShow: function () {
        this.setData({
            cutImg: app.globalData.cutImg,
        })
    },
    //获取code
    getCode() {
        wx.login({
            success: res => {
              if (res.code) {
                  console.log('getCode code:', res.code)
                  this.setData({
                      code: res.code
                  })
              }
            }
        })
    },
    //获取用户信息
    bindgetuserinfo(e) {
        var detail = e.detail
        console.log('bindgetuserinfo  e.detail: ', detail);
        if (detail.userInfo) {
            this.addPicFn();
        }
    },
    nameInputHandle(e) {
        var name = e.detail.value;
        this.setData({
            userName: this.trimStr(name),
        })
        console.log(this.data.userName)
    },
    trimStr(str) {
        return str.replace(/(^\s*)|(\s*$)/g, "");
    },
    addPicFn() {
        var _this = this
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['original'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function (res) {
                app.globalData.tempImg = res.tempFilePaths[0]
                wx.navigateTo({
                    url: '../../cutInside/cutInside',
                })
            },
            fail: function (error) {
                console.log(error)
            }
        })
    },
    longpress() {
        var _this = this
        if (this.data.cutImg == '') {
            wx.showToast({
                title: '请先选择背景图片',
                icon: 'none'
            })
        } else if (this.data.userName == '') {
            wx.showToast({
                title: '请先输入您的真实姓名',
                icon: 'none'
            })
        } else {
            this.setData({
                showModal: true
            })
        }
    },
    savaHandle() {
        var _this = this
        _this.drawCanvas();
        this.closeModal()
    },
    drawCanvas() {
        var ctx = wx.createCanvasContext('myCanvas');
        ctx.width = 319;
        ctx.height = 568;
        wx.showLoading({
            title: '图片生成中...',
            mask: true
        })
        ctx.clearRect(0, 0, 317, 568)
        ctx.save()
        ctx.drawImage(this.data.cutImg, 0, 0, 319, 317)
        ctx.drawImage('../../img/partake_bg.png', 0, 317, 319, 251)
        ctx.drawImage('../../img/wxapp_code.jpg', 105, 280, 110, 110)

        var canvasWidth = 319;
        var font15TotalNum = 21;
        var fontWidth = canvasWidth / font15TotalNum;
        var a = '我是';
        var aLen = a.length;
        var b = this.data.userName;
        var bLen = b.length;
        var totalLen = aLen + bLen;
        var aLeft = (font15TotalNum - totalLen) / 2 * fontWidth;
        // var bLeft = ((font15TotalNum - totalLen) / 2 + 2) * fontWidth + fontWidth / 2;
        ctx.setFontSize(15)
        ctx.setFillStyle('#0E0F28')
        ctx.fillText(a + ' ' + b, aLeft, 420)
        ctx.restore()
        var _this = this
        ctx.draw(false, function () {
            _this.savePic()
        })
    },
    savePic() {
        var _this = this
        var pixelRatio = this.data.pixelRatio
        wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: 319,
            height: 568,
            destWidth: 319 * pixelRatio,
            destHeight: 568 * pixelRatio,
            fileType: 'png',
            canvasId: 'myCanvas',
            quality: 1,
            success: function (res) {
                _this.setData({
                    imgSrc: res.tempFilePath
                })
                // app.globalData.imgSrcAry.push(res.tempFilePath)
                console.log(res.tempFilePath)
                wx.hideLoading()
                wx.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                    //上报分享信息
                    success(res) {
                        wx.showToast({
                            title: '已保存到相册',
                            icon: 'none'
                        })
                    },
                    fail(res) {
                        wx.showModal({
                            title: '生成代言图片失败',
                            content: '需要获取调用系统相册权限，请到小程序的设置中打开授权',
                            confirmText: '去授权',
                            confirmColor: '#FF6386',
                            success: function (type) {
                                if (type.confirm) {
                                    if (wx.openSetting) {
                                        wx.openSetting({});
                                    } else {
                                        wx.showModal({
                                            title: '提醒',
                                            content: '微信版本过低，请升级后体验',
                                            showCancel: false
                                        })
                                    }
                                }
                            },
                            fail: function (e) {
                                console.log(e)
                            }
                        })
                    }
                })

            },
            fail: function (res) {
                console.log(res)
            }
        })
    },
    preventTouch() {
        return false;
    },

    changeLookMore() {
        this.setData({
            showMore: !this.data.showMore
        })
    },

    closeModal() {
        this.setData({
            showModal: false
        })
    },
    goPreview() {
        wx.navigateTo({
            url: '../preview/preview'
        })
    },
    onShareAppMessage: function () {

    }
})